package de.fhb.itunes.controller.web;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.tomcat.util.http.fileupload.FileItem;
import org.apache.tomcat.util.http.fileupload.FileItemFactory;
import org.apache.tomcat.util.http.fileupload.FileUploadException;
import org.apache.tomcat.util.http.fileupload.disk.DiskFileItemFactory;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;

import com.sun.xml.internal.fastinfoset.sax.Properties;

import de.fhb.itunes.manager.Config;
import de.fhb.itunes.manager.ItunesUpdate;
import de.fhb.itunes.manager.Datenverwaltung.Albenverwaltung;
import de.fhb.itunes.manager.Datenverwaltung.Musikverwaltung;
import de.fhb.itunes.manager.filewatcher.OrdnerUebweachung;
import de.fhb.itunes.manager.mail.Mail;
import de.fhb.itunes.manager.mail.Template.Template;
import de.fhb.itunes.manager.mail.Template.TemplateBuilder;
import de.fhb.itunes.repository.data.Album_VO;
import de.fhb.itunes.repository.data.Musik_VO;

/**
 * Servlet implementation class upload
 */
@WebServlet("/upload")
public class upload extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public upload() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String nextJSP = "/login.jsp";
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object objtmp = session.getAttribute("loggedIn");
			if (objtmp != null) {
				boolean isLoggedIn = (Boolean) objtmp;
				if (isLoggedIn) {// session.isNew()
					nextJSP = "/upload.jsp";
					
				}
			}
		}
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		String nextJSP = "/login.jsp";
		HttpSession session = request.getSession(false);
		if (session != null) {
			Object objtmp = session.getAttribute("loggedIn");
			if (objtmp != null) {
				boolean isLoggedIn = (Boolean) objtmp;
				if (isLoggedIn) {// session.isNew()
					nextJSP = "/upload.jsp";
					FileItemFactory factory = new DiskFileItemFactory();
					ServletFileUpload upload = new ServletFileUpload(factory);
					Config config = new Config();
					try {
						List<FileItem> fields = upload.parseRequest(request);
						Iterator<FileItem> it = fields.iterator();
						if (!it.hasNext()) {
							System.out.println("no files found");
							return;
						}
						while (it.hasNext()) {
							FileItem fileItem = it.next();
							String parent = config.getProperties().getProperty("upload_path");
							File file = new File(parent+fileItem.getName());
							fileItem.write(file);
							request.setAttribute("dateiname", fileItem.getName());
						}
					} catch (FileUploadException e) {
						e.printStackTrace();
					}catch (Exception ex) {
						ex.printStackTrace();
					}
					
					
				}
			}
		}
		RequestDispatcher dispatcher = getServletContext()
				.getRequestDispatcher(nextJSP);
		dispatcher.forward(request, response);
	}
	

}
